//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Instrument.h"
using namespace Cephei::QL;
#include <gen/QL/PricingEngine.h>
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::CInstrument::CInstrument (boost::shared_ptr<QuantLib::Instrument>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInstrument = NULL;
#endif
	_ppInstrument = &childNative;
    
}
Cephei::QL::CInstrument::CInstrument (QuantLib::Instrument& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phInstrument = NULL;
#endif
	_ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (&childNative);
    
    _InstrumentOwner = owner;
    
}

Cephei::QL::CInstrument::CInstrument (CInstrument^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInstrument = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (copy->GetShared());
        
    }
}
Cephei::QL::CInstrument::CInstrument (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInstrument = NULL;
#endif
	if (!t->IsSubclassOf(CInstrument::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CInstrument::CInstrument (QuantLib::Handle<QuantLib::Instrument>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInstrument = &childNative;
	_ppInstrument = &static_cast<boost::shared_ptr<QuantLib::Instrument>>(childNative.currentLink());
    
    _InstrumentOwner = owner;
}
Cephei::QL::CInstrument::CInstrument (QuantLib::Handle<QuantLib::Instrument> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInstrument = &childNative;
	_ppInstrument = &static_cast<boost::shared_ptr<QuantLib::Instrument>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CInstrument::CInstrument (QuantLib::Instrument childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInstrument = NULL;
#endif
	_ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (new QuantLib::Instrument (childNative));
    
}
#endif

Cephei::QL::CInstrument::~CInstrument ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppInstrument != NULL)
    {
	    delete _ppInstrument;
        _ppInstrument = NULL;
    }
}
Cephei::QL::CInstrument::!CInstrument ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppInstrument != NULL)
    {
	    delete _ppInstrument;
    }
}
QuantLib::Instrument& Cephei::QL::CInstrument::GetReference ()
{
    if (_ppInstrument == NULL) throw gcnew NativeNullException ();
	return **_ppInstrument;
}
boost::shared_ptr<QuantLib::Instrument>& Cephei::QL::CInstrument::GetShared ()
{
    if (_ppInstrument == NULL) throw gcnew NativeNullException ();
	return *_ppInstrument;
}
QuantLib::Instrument* Cephei::QL::CInstrument::GetPointer ()
{
    if (_ppInstrument == NULL) throw gcnew NativeNullException ();
	return &**_ppInstrument;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Instrument>& Cephei::QL::CInstrument::GetHandle ()
{
	if (_phInstrument == NULL)
	{
		_phInstrument = new Handle<QuantLib::Instrument> (*_ppInstrument);
	}
	return *_phInstrument;
}
#endif
bool Cephei::QL::CInstrument::HasNative () 
{
	return (_ppInstrument != NULL);
}

DateTime Cephei::QL::CInstrument::ValuationDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppInstrument)->valuationDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CInstrument::ErrorEstimate::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInstrument)->errorEstimate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::CInstrument::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppInstrument)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CInstrument::NPV::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInstrument)->NPV ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IInstrument^ Cephei::QL::CInstrument::SetPricingEngine (Cephei::QL::IPricingEngine^ prm1)
{
    CPricingEngine^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CPricingEngine^> (prm1);
        _Cprm1->Lock();
        boost::shared_ptr<QuantLib::PricingEngine>& _prm1 = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_Cprm1->GetShared ()); 
    	(*_ppInstrument)->setPricingEngine ( _prm1 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

